package com.coscon.utils.thymeleafTaglib;


import com.coscon.common.SpringContextUtils;
import com.coscon.service.VelocityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.model.IText;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * @Description:  irca 自定义组合按钮
 * @Author: 409390047@qq.com
 * @Date: 2019/6/10 7:45
 */
@Slf4j
public class IrcaButtonTag extends AbstractElementTagProcessor {


    private static final String TAG_NAME = "ircaButton";



    private VelocityService velocityService;
    private static final int PRECEDENCE = 10000;

    public IrcaButtonTag(String dialectPrefix) {
        super(
                // 此处理器将仅应用于HTML模式
                TemplateMode.HTML,
                // 要应用于名称的匹配前缀
                dialectPrefix,
                // 标签名称：匹配此名称的特定标签
                TAG_NAME,
                // 将标签前缀应用于标签名称
                true,
                // 无属性名称：将通过标签名称匹配
                null,
                // 没有要应用于属性名称的前缀
                false,
                // 优先(内部方言自己的优先)
                PRECEDENCE);
    }


    public IrcaButtonTag(TemplateMode templateMode, String dialectPrefix, String elementName, boolean prefixElementName, String attributeName, boolean prefixAttributeName, int precedence) {
        super(templateMode, dialectPrefix, elementName, prefixElementName, attributeName, prefixAttributeName, precedence);
    }


    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag iProcessableElementTag, IElementTagStructureHandler structureHandler) {
        if (velocityService == null) {
            velocityService = SpringContextUtils.getBeanByClass(VelocityService.class);
        }
        String id = iProcessableElementTag.getAttributeValue("id");
        if(StringUtils.isBlank(id)){//如果id为空，这随机个
            id = StringUtils.replace( UUID.randomUUID().toString(),"-" ,"");
        }
        String name = iProcessableElementTag.getAttributeValue("name");
        if(StringUtils.isBlank(name)){
            name = id;
        }

        String cssclass = iProcessableElementTag.getAttributeValue("class");
        if(StringUtils.isBlank(cssclass)){
            cssclass = "";
        }
        //单击动作
        String onclick = iProcessableElementTag.getAttributeValue("onclick");
        if(StringUtils.isBlank(onclick)){
            onclick = "";
        }

        //按钮的文字
        String text = iProcessableElementTag.getAttributeValue("text");
        if(StringUtils.isBlank(text)){
            text = "";
        }
        //按钮上图标
        String ico = iProcessableElementTag.getAttributeValue("ico");
        if(StringUtils.isBlank(ico)){
            ico = "";
        }

        //按钮是否为连续执行
        String continuity = iProcessableElementTag.getAttributeValue("continuity");
        if(StringUtils.isBlank(continuity)){
            continuity = "false";
        }

        String elementStyle = iProcessableElementTag.getAttributeValue("style");
        if(StringUtils.isBlank(elementStyle)){
            elementStyle ="";
        }

        String keyword = iProcessableElementTag.getAttributeValue("keyword");
        if(StringUtils.isBlank(keyword)){
            keyword ="";
        }


        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("name", name);
        param.put("style", elementStyle);
        param.put("text", text);
        param.put("class", cssclass);
        param.put("onclick", onclick);
        param.put("ico", ico);
        param.put("continuity", BooleanUtils.toBoolean(continuity));
        param.put("keyword", keyword);

        //根据type选择需要加载的html、script
        String html = velocityService.getVmStringByResourceName("ircaButton.vm", param);

        IModelFactory modelFactory = context.getModelFactory();
        IModel model = modelFactory.createModel();

        IText htmlText = modelFactory.createText(html);
        model.add(htmlText);
        structureHandler.replaceWith(model, false);
    }




}
